/**
 * Created on Apr 20, 2000
 */
package jvs.ftp.server;

import java.net.ServerSocket;
import java.net.Socket;

import jvs.vfs.FileSystem;
import x.java.io.File;


/**
 * @author qiangli
 *
 */
public class FtpServer extends Thread {
	private static int serverId = 0;
	private int instanceId = 0;
	private int port = -1;

	private static FileSystem fs = FileSystem.getFileSystem();
	private static File root = fs.getRoot(); 
	
	public FtpServer(int port) {
		this.port = port;
		
	}
	
	public FtpServer(String root) {
		this.port = 21;
	}
 
	public void run() {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(port);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		serverId++;
		while (true) {
			try {
				System.out.println("JAVA FTP Server by Li Qiang. ["+serverId+":"+(++instanceId)+"]");
				Socket s = ss.accept();
				PI pi = new PI(s,root.toString());
				new Thread(pi).start();
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(1);
			}
		}
	}

	public static void main(String[] args) {  
		int port = 21;
		
		for (int i=0; i<args.length; i++) {
			if (args[i].equals("-port")) {
				port = Integer.parseInt(args[++i]);
			}
		}
		new FtpServer(port);
	}
}